﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using IntfClass;

namespace IntfSetting.FrmIntf
{
    public partial class FrmFreStep4 : Form
    {
        public FrmFreStep4(string ParentID)
        {
            InitializeComponent();
            if (!ParentID.Equals(string.Empty))
            {
                //blnNew = false;
                strParentID = ParentID;

            }

        }

        public DataTable dtGridView = new DataTable();
        public ArrayList arrWeekDay = new ArrayList();
        public string strWeek = string.Empty;

        private string strParentID = string.Empty;


        private void btnNew_Click(object sender, EventArgs e)
        {
            FrmFreStep3 frmFreStep3 = new FrmFreStep3(string.Empty,string.Empty,2);
            if (frmFreStep3.ShowDialog() == DialogResult.OK)
            {
                DataRow drRow = dtGridView.NewRow();
                drRow["StartTime"] = frmFreStep3.strTime;
                dtGridView.Rows.Add(drRow);
            }
        }

        private void SearchStartTime()
        {
            StringBuilder strSql = new StringBuilder();
            if (strParentID.Equals(string.Empty))
            {
                strSql.AppendLine("select * from TRuleList t where t.ParentID = 0");
            }
            else
            {
                strSql.AppendLine("select * from TRuleList t where t.ParentID = " + strParentID);
            }
            ClassDB classDB = new ClassDB();
            dtGridView = classDB.ExecuteDataTable(strSql.ToString());

        }

        private void FrmFreStep4_Load(object sender, EventArgs e)
        {
            SearchStartTime();

            string[] strWeekDay = strWeek.Split(',');
            if (strWeekDay.Length > 0)
            {
                for (int i = 0; i < strWeekDay.Length; i++)
                {
                    switch (strWeekDay[i])
                    {
                        case "1":
                            this.checkBox1.Checked = true;
                            break;
                        case "2":
                            this.checkBox2.Checked = true;
                            break;
                        case "3":
                            this.checkBox3.Checked = true;
                            break;
                        case "4":
                            this.checkBox4.Checked = true;
                            break;
                        case "5":
                            this.checkBox5.Checked = true;
                            break;
                        case "6":
                            this.checkBox6.Checked = true;
                            break;
                        case "7":
                            this.checkBox7.Checked = true;
                            break;
                        default:
                            break;
                    }
                }   
            }

            this.dataGridView1.DataSource = dtGridView;
            dataGridView1.Columns[0].Visible = false;
            dataGridView1.Columns[1].Visible = false;
            dataGridView1.Columns[2].Visible = true;

            dataGridView1.Columns[2].ReadOnly = true;

            dataGridView1.Columns[2].HeaderText = "开始时间";
        }

        private void btnUpt_Click(object sender, EventArgs e)
        {
            FrmFreStep3 frmFreStep3 = new FrmFreStep3(dataGridView1.SelectedRows[0].Cells["StartTime"].Value.ToString(),
                dataGridView1.SelectedRows[0].Cells["EndTime"].Value.ToString(),2);
            if (frmFreStep3.ShowDialog() == DialogResult.OK)
            {
                dataGridView1.SelectedRows[0].Cells["StartTime"].Value = frmFreStep3.strTime;
            }
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            dtGridView.Rows.RemoveAt(dataGridView1.SelectedRows[0].Index);

        }

        private void btnCancle_Click(object sender, EventArgs e)
        {
            if (arrWeekDay.Count == 0)
            {
                MessageBox.Show("未选择执行日");
                return;
            }

            if (dtGridView.Rows.Count == 0)
            {
                MessageBox.Show("未设定开始时间");
                return;
            }

            this.arrWeekDay.Sort();
            strWeek = string.Empty;
            foreach (var item in arrWeekDay)
            {
                strWeek = strWeek + item.ToString() + ",";

            }
            strWeek = strWeek.Remove(strWeek.Length - 1);
            this.Dispose();

        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            string strTag = ((CheckBox)sender).Tag.ToString();
            int index = arrWeekDay.IndexOf(strTag);

            if (((CheckBox)sender).Checked)
            {
                if (index < 0)
                {
                    arrWeekDay.Add(strTag);
                }

            }
            else
            {
                if (index >= 0)
                {
                    arrWeekDay.RemoveAt(index);
                }
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            this.dtGridView.Clear();
            this.arrWeekDay.Clear();
            this.Dispose();

        }

    }
}
